package com.redriver.hospitalsystem.systemgetway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.util.List;

@Component
public class MyGlobaleFilter implements GlobalFilter {

    @Resource
    private TokenTemplate tokenTemplate;
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        String uri = exchange.getRequest().getPath().toString();
        System.out.println(uri);
        if (uri.endsWith("login")||uri.endsWith("api-docs")||uri.endsWith("aliPay")||uri.endsWith("createSerial")) {
            return chain.filter(exchange);
        }else {
            List<String> userToken = exchange.getRequest().getHeaders().get("UserToken");
            tokenTemplate.parseToken(userToken.get(0));
            return chain.filter(exchange);
        }
    }
}
